package com.ufop.controller;

import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.comm.CommPortIdentifier;
import javax.comm.SerialPort;

import com.ufop.model.Dispositivo;

public class DispositivoController implements ActionListener {
	private Dispositivo disp;
			
	public DispositivoController(Dispositivo disp){
		this.disp = disp;
	}
	
	public void setSerial(String portID){
		disp.setpID(portID);
	}
	
	public void setMensagem(String msg){
		disp.setMessageAreaOut(msg);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		String cmd = e.getActionCommand();
		// Opens a port.
		if (cmd.equals("AbrirPorta")) {			
			try {
				disp.abrirConexao();
			} catch (Exception e1) {
				System.out.println("Error Opening Port!\n" +
						"Error opening port\n" +
						e1.getMessage() + "." +
						"\nSelect new settings, try again.");
				return;
			}
		}
		// Closes a port.
		else if (cmd.equals("FecharPorta"))
			disp.fecharConexao();
		else if (cmd.equals("limpar"))
			disp.limpaMensagens();
		
		
		
	}

}
